package cn.edu.neusoft.maxingxin231.yuejian06.Activity;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;



import cn.edu.neusoft.maxingxin231.yuejian06.R;

public class ApplyActivity extends BaseActivity {
    String name,price,user_name,num;
    TextView tv_detail,tv_name,tv_price,activityname,username,telephone,buynum;
    EditText ed_uname,ed_phone,ed_num;
    ImageButton imgbtn_return,imgbtn_collect;
    Button btn_buy;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_apply);
        imgbtn_return = (ImageButton)findViewById(R.id.imgbtn_return);
        imgbtn_collect = (ImageButton)findViewById(R.id.imgbtn_collect);
        tv_name = (TextView)findViewById(R.id.tv_name);
        tv_detail = (TextView)findViewById(R.id.tv_detail);
        tv_price = (TextView)findViewById(R.id.tv_price);
        ed_uname=(EditText)findViewById(R.id.ed_uname);
        ed_phone=(EditText)findViewById(R.id.ed_phone);
        ed_num=(EditText)findViewById(R.id.ed_num);
        btn_buy=(Button)findViewById(R.id.btn_buy);
        name=getIntent().getStringExtra("name");
        price=getIntent().getStringExtra("price");
        tv_name.setText(name);
        tv_price.setText("￥"+price+"元");


    }

    @Override
    void initEvent() {
        btn_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ed_uname.getText().toString().trim().equals("") || ed_phone.getText().toString().trim().equals("")){
                    showToast("不能为空");
                }else {
                    String user_name=ed_uname.getText().toString();
                    String user_phone=ed_phone.getText().toString();
                    String  num=ed_num.getText().toString();
                }
            }
        });
        ed_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String num=ed_num.getText().toString();
                if (num.equals(""))
                    tv_price.setText(price);
                else {
                    float toral=sum(num);
                    String str=String.valueOf(toral);
                    tv_price.setText(str+"");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        imgbtn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //购买按钮
        btn_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ApplyActivity.this,ApplySuccessActivity.class);
                startActivityForResult(intent,1);
            }
        });

    }

    @Override
    void initData() {

    }
    private float sum(String num){
        if(num.equals("")) {return 0;}
        int money=Integer.parseInt(num);
        Float single=Float.parseFloat(price);
        return money*single;
    }
}
